//
//  MessageViewController.m
//  电影豆瓣--完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MessageViewController.h"
#import "UIImageView+WebCache.h"
#import "DataBaseHandle.h"
#import "MyModel.h"
#import "UserModel.h"
@interface MessageViewController ()

@property (nonatomic, assign) CGFloat height;

@property (nonatomic, retain) UIScrollView *scrollView;


@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.model.title;
    
    [self createBarButtonItem];
    [self createScrollView];
    
    
}


- (void)item1Action:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 收藏
- (void)item2Action:(UIBarButtonItem *)item {
    
    [[DataBaseHandle sharedDataBaseHandle] createTable];
    
    // 从数据库中查询是否有用户登录
    UserModel *model = [[DataBaseHandle sharedDataBaseHandle] selectUserState:@"是"];
    if (model.userName == nil) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户未登录" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController: alert animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        // 将点击的活动传给新建的这个model，来从数据库中查询
        MyModel *model1 = [MyModel myModel];
        model1.title = self.model.title;
        model1.userName = model.userName;
        // 按model1查询
        MyModel *tempModel = [[[DataBaseHandle sharedDataBaseHandle] selectModel:model1 table:@"Activity"] lastObject];
        
        // 如果没找到（即不存在），则添加
        if (tempModel == nil) {
            [[DataBaseHandle sharedDataBaseHandle] insertModel:model1 table:@"Activity"];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
            
        } else {
            // 如果存在，则从数据库中删除
            [[DataBaseHandle sharedDataBaseHandle] deleteModel:model1 table:@"Activity"];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }

    }
    
    
    
}

- (void)createBarButtonItem {
    // 返回的UIBarButtonItem（左）
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 0, 30, 30);
    [button1 setImage:[UIImage imageNamed:@"iconfont-xiangzuo.png"] forState:UIControlStateNormal];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
    [button1 addTarget:self action:@selector(item1Action:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = item1;
    
    // 收藏的UIBarButtonItem（右）
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, 0, 30, 30);
    [button2 setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
    [button2 addTarget:self action:@selector(item2Action:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = item2;
}

- (void)createScrollView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    //************************内容展示******************************************
    
    // 数据解析
    // 详情信息
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 340, 50)];
    [self.scrollView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [titleLabel release];
    titleLabel.text = self.model.title;
    // 图片
    UIImageView *imagView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 70, 130, 200)];
    [self.scrollView addSubview:imagView];
    [imagView sd_setImageWithURL:[NSURL URLWithString:self.model.image]];
    [imagView release];
    
    // 小图标和文字
    UIImageView *imag1 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 75, 25, 25)];
    UIImageView *imag2 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 110, 25, 25)];
    UIImageView *imag3 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 145, 25, 25)];
    UIImageView *imag4 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 180, 25, 25)];
    [self.scrollView addSubview:imag1];
    [self.scrollView addSubview:imag2];
    [self.scrollView addSubview:imag3];
    [self.scrollView addSubview:imag4];
    imag1.image = [UIImage imageNamed:@"icon_date_blue.png"];
    imag2.image = [UIImage imageNamed:@"icon_sponsor_blue.png"];
    imag3.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    imag4.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    
    UILabel *ll1 = [[UILabel alloc] initWithFrame:CGRectMake(170, 75, 220, 30)];
    UILabel *ll2 = [[UILabel alloc] initWithFrame:CGRectMake(170, 110, 200, 30)];
    UILabel *ll3 = [[UILabel alloc] initWithFrame:CGRectMake(170, 145, 200, 30)];
    UILabel *ll4 = [[UILabel alloc] initWithFrame:CGRectMake(170, 180, 200, 50)];
    [self.scrollView addSubview:ll1];
    [self.scrollView addSubview:ll2];
    [self.scrollView addSubview:ll3];
    [self.scrollView addSubview:ll4];
    ll1.text = [NSString stringWithFormat:@"%@--%@", self.model.beginTime, self.model.endTime];
    ll2.text = result[@"events"][self.index.row][@"owner"][@"name"];
    ll3.text = [NSString stringWithFormat:@"类型：%@", self.model.category_name];
    ll4.numberOfLines = 2;
    ll4.text = self.model.address;

    
    
    NSString *text =  result[@"events"][self.index.row][@"content"];
    //*********************************************************************************************************************************************************************************
    // 获取文字大小
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    // 通过文字大小 获取文本的高度
    CGRect rect = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    _height = rect.size.height;
    //**********************************************************************************************************************************************************************************
    // 活动介绍的label
    UILabel *huodong = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, 100, 50)];
    huodong.text = @"活动介绍";
    huodong.font = [UIFont systemFontOfSize:20];
    [self.scrollView addSubview:huodong];
    [huodong release];
    // 活动介绍
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 360, self.view.frame.size.width - 20, _height)];
    [self.scrollView addSubview:label];
    label.font = [UIFont systemFontOfSize:17];
    [label release];
    label.text = text;
    label.numberOfLines = 10000;
  
     self.scrollView.contentSize = CGSizeMake(0, _height + 360 +  79);
    
  
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
